// pages/cancelCase/cancelCase.js
const allUrl = require('../../config').allUrl
var app = getApp()

Page({
  data:{
    buttonTitle: '确定取消',
    content: [],
    isopen: false, // 是否打开  
    showText: '', //  文本框中的文本信息
    selectId: 1,  //  选中的理由的id
    index: 0, //选中的index
  },


  onLoad:function(options){
    var that = this
    this.getCancelListReq(function(backArr){
      that.setData({
        selectId: backArr[0].id,
        showText: that.data.content.length>0?that.data.content[0].name:''
      })
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },


//  滑动选择了一条取消理由
  bindPickerChange:function(e){
    console.log(e.detail.value)
    this.setData({
      index: e.detail.value,
      selectId: this.data.content[e.detail.value].id,
    })
  },
//点击取消案件
  sendNumb: function(e){
    //点击取消案件
    // console.log('点击取消案件'+this.data.selectId)
    wx.showToast({
      title: "正在取消案件...",
      icon: "loading",
      duration: 100000
    })
    // if(app.globalData.call_id){
      //  发送取消案件请求
      this.sendCancelReq(function(){
        app.globalData.call_id = null
        //  返回到案件列表
        wx.switchTab({
          url: '../caselist/caselist',
        })
      })
    // }else{
    //   wx.hideToast()
    // }
  },


  //  获取取消理由列表
  getCancelListReq: function(successBack){
    var that = this
    wx.request({
      url: allUrl + 'Api/getCancelList.html',
      header: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        type: 1
      },
      method: 'POST', 
      success: function(res){
        console.log(res.data)
        if(res.data.status_code == 200){
          that.setData({
            content: res.data.data.cancelList,
          })
          successBack(res.data.data.cancelList)
        }
      },
      fail: function(res) {},
      complete: function(res) {}
    })
  },

  //  发送取消案件请求
  sendCancelReq: function(successBack){
    var that = this
    wx.request({
      url: allUrl + 'Api/setCallCancel.html',
      header: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        call_id: app.globalData.call_id,
        cancel_id: that.data.selectId
      },
      method: 'POST', 
      success: function(res){
        // console.log('res.data????????')
        // console.log(res.data)
        if(res.data.status_code == 200){
          //  取消完成
          successBack()
        } else {
          wx.showToast({
            title: res.data.msg + '',
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {wx.hideToast()}
    })
  },

})